package com.example.fasttask;

import java.text.DateFormat;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;

public class EditTaskActivity extends Activity {
	
	TaskTranslator translator;
	Spinner spinner;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_edit_task);
		
		translator = new TaskTranslator(this);
		translator.open();
		
		spinner = (Spinner) findViewById(R.id.spinner1);
		// Create an ArrayAdapter using the string array and a default spinner layout
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
		        R.array.times_array, android.R.layout.simple_spinner_item);
		// Specify the layout to use when the list of choices appears
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// Apply the adapter to the spinner
		spinner.setAdapter(adapter);
		
		
	}
	
	public void discard(View v){
		finish();
	}
	
	public void newtask(View v){
		
		
		EditText nameview = (EditText) findViewById(R.id.editText1);
		Editable nameedit = nameview.getText();

		CheckBox recurview = (CheckBox) findViewById(R.id.checkBox1);
		String time = (String) spinner.getSelectedItem();
		String [] timearray = time.split(" ");
		if(timearray[0].equals("select")) {
			//then the user input the length wrong
			AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
			
			alertDialogBuilder.setTitle("No time set!");
			
			alertDialogBuilder
				.setMessage("You didn't set a length of time for your task!")
				.setCancelable(false)
				.setNeutralButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.dismiss();
					}
				});
			
			AlertDialog alertDialog = alertDialogBuilder.create();
			alertDialog.setOwnerActivity(this);
			
			alertDialog.show();
		} else { 
		int length = Integer.parseInt(timearray[0]);
		Date date = new Date();
		DateFormat df = DateFormat.getDateInstance();
		
		
		
		//Log.d("asdf", time);
		

		//Task t = translator.createTask(nameedit.toString(), length, date, recurview.isChecked());
		
		//Log.d("asdf", "task created");

		Bundle bundle = new Bundle();
		Intent data = new Intent("my.action");

		data.putExtra("name", nameedit.toString());
		data.putExtra("length", length + "");
		data.putExtra("date", df.format(date));
		data.putExtra("recur", recurview.isChecked() + "");

		sendBroadcast(data);
		

		

//        Intent intent = new Intent(EditTaskActivity.this, EditListActivity.class);
//		
//		Bundle bundle = new Bundle();
//        bundle.putString("name", "nameedit.toString");
//        bundle.putString("length", length + "");
//        bundle.putString("date", df.format(date));
//        bundle.putString("recur", recurview.isChecked() + "");
////        
//        intent.putExtras(bundle);
//        startActivity(intent);
		finish();
		}
		
		
	}
	
	public void startEditListActivity(View view) {
		Intent intent = new Intent(EditTaskActivity.this, EditListActivity.class);
        startActivity(intent);
        finish();
	}
}
